<?php if (!defined('BASEPATH')){ exit('No direct script access allowed'); }

/**
 *  Stats Controller
 *
 * @package XGO CMS v3.0
 * @subpackage Stats
 * @link http://sunsoft.vn
 */

class Stats extends MY_Controller {

	public function __construct() {
		parent::__construct();
		require_once(APPPATH . 'modules/backend/autoload.php');
		if($this->is_logged_in() == FALSE) {
			$this->session->set_userdata('redirect_uri', current_url());
			redirect('auth');
		}else {
			$this->load->language('stats');
		}
		$this->output->enable_profiler(ENABLE_PROFILER);
                $this->load->model('adminlog_model');
	}

	/**
	 * Displays list of category.
	 */
	public function index(){
		$this->view_data	= array();
		$this->view_data['flash_message_error']	= $this->session->flashdata('flash_message_error');
		$this->view_data['flash_message']		= $this->session->flashdata('flash_message');
		$this->view_data['role_by_group'] 		= array();
		if($this->database_connect_status){
//                    if($this->session->userdata['group_id'] == 1){
                        $this->load->model('adminlog_static1_model');
                        $this->view_data['sales']               = new stdClass();
                        $s_sales                                =   $this->adminlog_model->statitic(date('Y-m-d'),'[Sales]');
                        $s_update                               =   $this->adminlog_model->statitic(date('Y-m-d'),'[SalesUpdate]');
                        $s_search                               =   $this->adminlog_model->statitic(date('Y-m-d'),'[SalesSearch]');
                        $s_login                                =   $this->adminlog_model->statitic(date('Y-m-d'),'[Login]');
                        $s_logout                               =   $this->adminlog_model->statitic(date('Y-m-d'),'[Logout]');
                        
                        $this->view_data['sales']->add          = $s_sales[0]->total;
                        $this->view_data['sales']->edit         = $s_update[0]->total;
                        $this->view_data['sales']->search       = $s_search[0]->total;
                        $this->view_data['sales']->login        = $s_login[0]->total;
                        $this->view_data['sales']->logout       = $s_logout[0]->total;
                        $this->view_data['sales']->date         = date("Y-m-d");
                        
                        $info                                   = $this->adminlog_static1_model->find_by(array('date'=>date("Y-m-d")));
                        if($info){
                            $this->adminlog_static1_model->update($this->view_data['sales'],$info[0]->id);
                        } else {
                            $this->adminlog_static1_model->create($this->view_data['sales']);
                        }
                        
                        $selectAll                                   = $this->adminlog_static1_model->selectAll(3);
                        
                        if(in_array($this->session->userdata['group_id'],array(1,76))){
                            $this->view_data['lists'] = '';
                            foreach($selectAll as $key => $vals){
                                if($key ==  count($selectAll)-1){
                                    $dot = "";
                                } else {
                                    $dot = ",";
                                }
                                $this->view_data['lists'] .= "['".date("d-m-Y", strtotime($vals->date))."', ".$vals->add.", ".$vals->edit.", ".$vals->search.", ".$vals->login.",".$vals->logout."]".$dot;
                            }
                        }
                        
                        //statitic users
                        //////////////$this->view_data['statiticUsers'] = $this->adminlog_model->statiticUsers();
//                    }
                        //chart phone
                        if(in_array($this->session->userdata['group_id'],array(1))){
                            $this->load->model("salesphone_model");
                            $this->view_data["phone"]           = new stdClass();
                            $this->view_data["phone"]->sum      = $this->salesphone_model->count_by();
                            $this->view_data["phone"]->invalid  = $this->salesphone_model->count_by(array("status"=>"0"));
                            $this->view_data["phone"]->waiting  = $this->salesphone_model->count_by(array("status"=>"2","waiting"=>"1"));
                            $this->view_data["phone"]->used     = $this->salesphone_model->count_by(array("status"=>"2","waiting"=>"0"));
                        }
                        
                        
			$this->load->model('stats_model');
			$user_info                              = $this->stats_model->get_user_info($this->stats_model->get_user_id());
			$this->view_data['role_by_group']       = $user_info['role_list'];
			$this->view_data['username']            = $user_info['username'];
			$this->load->view('auth/stats/dashboard', $this->view_data);
		}else{
			$this->view_data['flash_message']	= $this->lang->line('database_connect_failed');
			$this->load->view('templates/backend/error', $this->view_data);
		}
	}	
	function list_news_hits(){
		$day = $this->input->post('day');			
		if($this->database_connect_status){		
			$this->load->model('stats_model');
			$this->load->model('news_model');
			$this->view_data['news_top_hits']	= $this->stats_model->get_list_news_hits($day);
			$this->load->view('auth/stats/list_news_hits',$this->view_data);
		}
	}
}

/* End of file statistic.php */
/* Location: ./application/controllers/auth/statistic.php */
